﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    //public GameObject prefabDeadAnim;
    Animator anim;
    CharacterController2D cc;
    Rigidbody2D rb2d;

    Transform stampPoint;
    TimeBack timeBack;
    float horizontalMove;
    bool jumpPressed;

    public float speed;

    public GameObject parent=null;  //父节点

    [Header("其他组件")]
    public GameObject Monster;
    public GameObject Magnet;
    public Rigidbody2D Player_Rb2d;


    [Header("按键参数")]
    bool isFacingLeft = false;
    bool usingMagnet = false;
    float magentIsUsingUp = 0f;
    float magentIsUsingDown = 0f;


    void Start()
    {
        anim = GetComponent<Animator>();
        //stampPoint = transform.Find("StampPoint");

        cc = GetComponent<CharacterController2D>();
        rb2d = GetComponent<Rigidbody2D>();
        Time.timeScale = 1f;
        timeBack = GetComponent<TimeBack>();
    }

    void Update()
    {
        Key();

        horizontalMove = Input.GetAxisRaw("Horizontal");
        horizontalMove *= speed;

        anim.SetFloat("xVelocity",Mathf.Abs(horizontalMove));
        anim.SetFloat("yVelocity",rb2d.velocity.y);
        jumpPressed = Input.GetButton("Jump");

        UseMagnet();
        FacingDirection();

    }

    private void FixedUpdate()
    {
        //暂停控制器的数据输入
        if (timeBack.isActioning) {
            return;
        }    
            
        cc.Move(horizontalMove, jumpPressed);
    }


    public void UseMagnet()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {

            magentIsUsingDown = 0f;
            magentIsUsingUp = 0f;

            if (usingMagnet == false)
            {
                Magnet.SetActive(true);
                //Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), LayerMask.NameToLayer("Player"), false);
                magentFacing();
                usingMagnet = true;
                return;
            }
            if (usingMagnet == true)
            {
                Magnet.SetActive(false);
                usingMagnet = false;
                Player_Rb2d.velocity = new Vector2(0f, 0f);
                //Player_Rb2d.gravityScale = 5f;
                //Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), LayerMask.NameToLayer("Player"), true);
                return;
            }

        }

        if (usingMagnet)
        {
            if (Input.GetKeyDown(KeyCode.W))
            {
                Magnet.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
                magentIsUsingUp++;
                magentIsUsingDown = 0f;
                if (magentIsUsingUp == 2f)
                {
                    magentFacing();
                    magentIsUsingDown = 0f;
                    magentIsUsingUp = 0f;
                }
            }
            if (Input.GetKeyDown(KeyCode.S))
            {
                Magnet.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 180f));
                magentIsUsingUp = 0f;
                magentIsUsingDown++;
                if (magentIsUsingDown == 2f)
                {
                    magentFacing();
                    magentIsUsingDown = 0f;
                    magentIsUsingUp = 0f;
                }
            }
        }
    }

    //磁铁朝向
    public void magentFacing()
    {
        if (isFacingLeft)
        {
            Magnet.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 90f));
        }
        else
        {
            Magnet.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -90f));
        }
    }
    #region
    public void Key()
    {
        if (Input.GetKey(KeyCode.R))
        {
            Restart();
        }
    }

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        Debug.Log("RestartGame");
       // isHurt = false;

    }

    public void FacingDirection()
    {
        if (horizontalMove < 0)
        {
            isFacingLeft = true;
        }
        if (horizontalMove > 0)
        {
            isFacingLeft = false;
        }
    }
    #endregion
}
